package com.minimalist.lindi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.http.HttpStatus;
import com.minimalist.common.config.exception.BusinessException;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.basic.vo.file.FileVO;
import com.minimalist.common.entity.lindi.po.LindiTradeApplicationApproveFile;
import com.minimalist.common.entity.lindi.vo.tradeapplicationapprovefile.LindiTradeApplicationApproveFileVO;
import com.minimalist.common.entity.lindi.vo.tradeapplicationapprovefile.TradeApplicationApproveFileQueryVO;
import com.minimalist.common.feign.client.basic.FileFeignClient;
import com.minimalist.common.response.R;
import com.minimalist.common.utils.UnqIdUtil;
import com.minimalist.lindi.mapper.LindiTradeApplicationApproveFileMapper;
import com.minimalist.lindi.service.LindiTradeApplicationApproveFileService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 林地指标交易平台_交易申请审批文件 服务层实现。
 *
 * @author 11987
 * @since 2025-06-07
 */
@Service
public class LindiTradeApplicationApproveFileServiceImpl extends ServiceImpl<LindiTradeApplicationApproveFileMapper, LindiTradeApplicationApproveFile> implements LindiTradeApplicationApproveFileService{

    private static final Logger log = LoggerFactory.getLogger(LindiTradeApplicationApproveFileServiceImpl.class);

    @Autowired
    private LindiTradeApplicationApproveFileMapper mapper;

    @Autowired
    private FileFeignClient fileFeignClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addTradeApplicationApproveFile(LindiTradeApplicationApproveFileVO tradeApplicationApproveFileVO){
        LindiTradeApplicationApproveFile record = BeanUtil.copyProperties(tradeApplicationApproveFileVO
                , LindiTradeApplicationApproveFile.class);
        record.setApproveFileId(UnqIdUtil.uniqueId());
        int insert = mapper.insert(record, true);
        Assert.isTrue(insert == 1, () -> new BusinessException(
                "添加交易申请审批文件失败"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTradeApplicationApproveFileByApproveFileId(Long approveFileId){
        LindiTradeApplicationApproveFile record = mapper.selectTradeApplicationApproveFileByApproveFileId(approveFileId);
        Assert.notNull(record, () -> new BusinessException("交易申请审批文件不存在"));
        int delete = mapper.delete(record);
        Assert.isTrue(delete == 1, () -> new BusinessException(
                "删除交易申请审批文件失败"));
        // 删除关联的文件
        if (record.getFileId() != null){
            try{
                R<Void> result = fileFeignClient.deleteFile(record.getFileId());
                if (result.getCode() != 200){
                    log.warn("删除关联文件失败，文件ID: {}, 错误信息: {}", record.getFileId(), result.getMessage());
                }
            } catch (Exception e){
                log.warn("删除关联文件异常，文件ID: {}, 异常信息: {}", record.getFileId(), e.getMessage());
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTradeApplicationApproveFileByApproveFileId(
            LindiTradeApplicationApproveFileVO tradeApplicationApproveFileVO){
        LindiTradeApplicationApproveFile record = mapper.selectTradeApplicationApproveFileByApproveFileId(
                tradeApplicationApproveFileVO.getApproveFileId());
        Assert.notNull(record, () -> new BusinessException("交易申请审批文件不存在"));
        BeanUtil.copyProperties(tradeApplicationApproveFileVO, record);
        int update = mapper.update(record);
        Assert.isTrue(update == 1, () -> new BusinessException(
                "修改交易申请审批文件失败"));
    }

    @Override
    public PageResp<LindiTradeApplicationApproveFileVO> getPageTradeApplicationApproveFileList(
            TradeApplicationApproveFileQueryVO queryVO){
        Page<LindiTradeApplicationApproveFileVO> page = mapper
                .getPageTradeApplicationApproveFileList(
                        Page.of(queryVO.getPageNum(), queryVO.getPageSize()), queryVO);
        List<LindiTradeApplicationApproveFileVO> records = page.getRecords();
        // 填充文件信息
        for (LindiTradeApplicationApproveFileVO record : records){
            if (record.getFileId() != null){
                R<FileVO> fileResult = fileFeignClient.getFileByFileId(record.getFileId());
                if (fileResult.getCode() == 200 && fileResult.getData() != null){
                    record.setFile(fileResult.getData());
                }
            }
        }
        return new PageResp<>(records, records.size());
    }

    @Override
    public LindiTradeApplicationApproveFileVO getTradeApplicationApproveFileByApproveFileId(Long approveFileId){
        LindiTradeApplicationApproveFileVO record = mapper
                .getTradeApplicationApproveFileByApproveFileId(approveFileId);
        Assert.notNull(record, () -> new BusinessException("交易申请审批文件不存在"));
        // 填充文件信息
        if (record.getFileId() != null){
            R<FileVO> fileResult = fileFeignClient.getFileByFileId(record.getFileId());
            if (fileResult.getCode() == HttpStatus.HTTP_OK
                    && fileResult.getData() != null){
                record.setFile(fileResult.getData());
            }
        }
        return record;
    }
}
